package com.quartz.mq.config;

import com.quartz.mq.infra.quartz.AutowiringJobFactory;
import com.quartz.mq.infra.quartz.QuartzJob;
import org.quartz.Scheduler;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Executor;

/**
 * <h3>partitionjob</h3>
 * <p>quartz</p>
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2019-12-19 17:29
 **/
@Configuration
public class QuartzConfig {

    // TODO 多台服务器的时间同步问题

    @Autowired
    private DataSource dataSource;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {
        SchedulerFactoryBean factory = new SchedulerFactoryBean();

        factory.setSchedulerName("Cluster_Scheduler");
        factory.setDataSource(dataSource);
        factory.setApplicationContextSchedulerContextKey("applicationContext");
        //设置任务执行器
        factory.setTaskExecutor(schedulerThreadPool());
        //设置触发器
        factory.setTriggers(quartzTrigger().getObject());
        //设置quartz属性
        factory.setQuartzProperties(quartzProperties());
        //这只job工厂
        factory.setJobFactory(autowiringJobFactory());
        factory.setOverwriteExistingJobs(true);
        factory.setStartupDelay(15);
        return factory;
    }

    /**
     * 功能描述: 
     * <自动注入工厂>
     * @param 
     * @date : 2019/12/19 17:37
     * @return : org.quartz.spi.JobFactory
     * @author : hduong
     */
    @Bean
    public JobFactory autowiringJobFactory() {
        return new AutowiringJobFactory();
    }

    /**
     * <调度器>
     * @param ，没有调度器 启动任务 任务也是不会执行的 ：
     * @date : 2019/12/19 17:37
     * @return : org.quartz.Scheduler
     * @author : hduong
     */
    @Bean
    public Scheduler scheduler() throws Exception {
        Scheduler scheduler = schedulerFactoryBean().getScheduler();
        scheduler.start();
        return scheduler;
    }

    /**
     * <获取 属性>
     * @param 
     * @date : 2019/12/19 17:40
     * @return : java.util.Properties
     * @author : hduong
     */
    @Bean
    public Properties quartzProperties() throws IOException {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));

        //在quartz.properties中的属性被读取并注入后再初始化对象
        propertiesFactoryBean.afterPropertiesSet();
        return propertiesFactoryBean.getObject();
    }

    /**
     * 任务定义信息
     * @param 
     * @date : 2019/12/19 17:56
     * @return : org.springframework.scheduling.quartz.JobDetailFactoryBean
     * @author : hduong
     */
    @Bean
    public JobDetailFactoryBean quartzJob() {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();

        jobDetailFactoryBean.setJobClass(QuartzJob.class);
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setGroup("order");
        jobDetailFactoryBean.setName("order_job");
        jobDetailFactoryBean.setRequestsRecovery(true);
        return jobDetailFactoryBean;
    }

    /**
     * quartz 触发器
     * @param 
     * @date : 2019/12/19 17:56
     * @return : org.springframework.scheduling.quartz.CronTriggerFactoryBean
     * @author : hduong
     */
    @Bean
    public CronTriggerFactoryBean quartzTrigger() {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();

        cronTriggerFactoryBean.setJobDetail(quartzJob().getObject());
        cronTriggerFactoryBean.setCronExpression("0/15 * * * * ?");
        return cronTriggerFactoryBean;
    }

    /**
     * 线程池配置
     * @param 
     * @date : 2019/12/19 17:59
     * @return : java.util.concurrent.Executor
     * @author : hduong
     */
    @Bean
    public Executor schedulerThreadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        executor.setCorePoolSize(15);
        executor.setMaxPoolSize(25);
        executor.setQueueCapacity(100);
        return executor;
    }
}
